package com.joe.xl.devcommon1.util.exception;

import com.joe.xl.devcommon1.util.result.EnumStatus;
import com.joe.xl.devcommon1.util.result.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author joexl <joe_xuli@163.com>
 * @Since 1.0
 * @Date 2020/11/10
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public JsonResult gloexHandler(HttpServletRequest servletRequest, Exception e){
//        e.printStackTrace();

        if(e instanceof GlobalException){
            GlobalException globalException = (GlobalException)e;

            return JsonResult.fail(globalException.toString());
        }

        return JsonResult.fail(EnumStatus.SERVER_ERROR);
    }
}
